package com.hzo.great.leisuregreat.views;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.base.BaseActivity;
import com.hzo.great.leisuregreat.constants.UrlConstant;
import com.hzo.great.leisuregreat.dialog.DialogFactory;
import com.hzo.great.leisuregreat.dialog.LoadingDialog;
import com.hzo.great.leisuregreat.utils.MyStringCallback;
import com.hzo.great.leisuregreat.utils.RequestSever;
import com.hzo.great.leisuregreat.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressManagement extends BaseActivity{

    RecyclerView list_view;

    TextView addReceiveAddress;

    ImageView back;

    RecycleViewAdapter recycleViewAdapter;


    List<Map<String,String>> listmap = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //fullScreen(this, true);
        setContentView(R.layout.dizhiguanli);
        getAddress();
        initView();
    }



    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.addReceiveAddress:
                startActivity(new Intent(AddressManagement.this,AddEditReceiveAddress.class));
                break;

            case R.id.back:
                AddressManagement.this.finish();
                break;
        }
    }

    public void initView() {

        list_view = (RecyclerView) findViewById(R.id.list_view);

        addReceiveAddress = (TextView) findViewById(R.id.addReceiveAddress);

        back = (ImageView) findViewById(R.id.back);

        recycleViewAdapter = new RecycleViewAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        list_view.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        list_view.setAdapter(recycleViewAdapter);

        addReceiveAddress.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    public void showToast(String toast) {
        Toast.makeText(this,toast,Toast.LENGTH_SHORT).show();
    }


    public void getAddress() {

        Map<String, Object> map = new HashMap<>();
        map.put("access_token", SharedPreferencesUtils.getString("access_token",""));
        map.put("customerId",SharedPreferencesUtils.getString("customerId",""));
        LoadingDialog.getInstance(this).showLoadDialog("");
        RequestSever.post(UrlConstant.GETADDRESS_URL,map,new MyStringCallback<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LoadingDialog.closeLoadDialog();
                JSONObject jsonObject = JSON.parseObject(result);
                switch (jsonObject.getIntValue("code")){
                    case 200:
                        listmap = JSON.parseObject(jsonObject.getString("data"),new TypeReference<List<Map<String, String>>>(){});
                        recycleViewAdapter.notifyDataSetChanged();
                        break;

                    default:
                        ShortToast(jsonObject.getString("message"));
                        break;
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LoadingDialog.closeLoadDialog();
            }
        });
    }

    private void ShortToast(String s) {
        Toast.makeText(AddressManagement.this,s,Toast.LENGTH_SHORT).show();
    }

    class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder>{

        @NonNull
        @Override
        public RecycleViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(AddressManagement.this).inflate(R.layout.item_dizhi, viewGroup, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull final RecycleViewAdapter.MyViewHolder holder, final int position) {

            holder.name.setText(listmap.get(position).get("ReseverName"));
            holder.phoneNum.setText(listmap.get(position).get("PhoneNo"));
            holder.address.setText(listmap.get(position).get("Address"));
            holder.setDefaultAddress.setChecked(Boolean.parseBoolean(listmap.get(position).get("IsDefault")));


            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogFactory.showAllDialog(AddressManagement.this,R.layout.delete_address,R.style.CustomDialog, 0, Gravity.CENTER, 0.7f, 0, new DialogFactory.DialogListener(){

                        @Override
                        public void OnInitViewListener(View v, final Dialog dialog) {
                            TextView no = (TextView) v.findViewById(R.id.no);
                            TextView yes = (TextView) v.findViewById(R.id.yes);

                            no.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });

                            yes.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    LoadingDialog.getInstance(AddressManagement.this).showLoadDialog("");
                                    Map<String, Object> map = new HashMap<>();
                                    map.put("access_token",SharedPreferencesUtils.getString("access_token",""));
                                    map.put("Id",listmap.get(position).get("Id"));
                                    RequestSever.post(UrlConstant.DELETEADDRESS_URL,map,new MyStringCallback<String>(){
                                        @Override
                                        public void onSuccess(String result) {
                                            super.onSuccess(result);
                                            LoadingDialog.closeLoadDialog();
                                            JSONObject jsonObject = JSON.parseObject(result);
                                            switch (jsonObject.getIntValue("code")){
                                                case 200:
                                                    getAddress();
                                                    break;

                                                default:
                                                    ShortToast(jsonObject.getString("message"));
                                                    break;
                                            }
                                        }
                                        @Override
                                        public void onError(Throwable ex, boolean isOnCallback) {
                                            super.onError(ex, isOnCallback);
                                            LoadingDialog.closeLoadDialog();
                                        }
                                    });
                                    dialog.dismiss();
                                }
                            });
                        }
                    } );
                }
            });

            holder.editor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent araIntent = new Intent();
                    araIntent.putExtra("name",listmap.get(holder.getAdapterPosition()).get("ReseverName"));
                    araIntent.putExtra("phoneNum",listmap.get(holder.getAdapterPosition()).get("PhoneNo"));
                    araIntent.putExtra("address",listmap.get(holder.getAdapterPosition()).get("Address"));
                    araIntent.putExtra("addressId",listmap.get(holder.getAdapterPosition()).get("Id"));
                    araIntent.setClass(AddressManagement.this,AddEditReceiveAddress.class);
                    startActivity(araIntent);
                }
            });

            holder.setDefaultAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LoadingDialog.getInstance(AddressManagement.this).showLoadDialog("");
                    Map<String, Object> map = new HashMap<>();
                    map.put("access_token", SharedPreferencesUtils.getString("access_token",""));
                    map.put("customerId",SharedPreferencesUtils.getString("customerId",""));
                    map.put("postId",listmap.get(position).get("Id"));
                    RequestSever.post(UrlConstant.SETDEFAULTADDRESS_URL,map,new MyStringCallback<String>(){
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            LoadingDialog.closeLoadDialog();
                            JSONObject jsonObject = JSON.parseObject(result);
                            switch (jsonObject.getIntValue("code")){
                                case 200:
                                    getAddress();
                                    break;

                                default:
                                    ShortToast(jsonObject.getString("message"));
                                    break;
                            }
                        }
                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            super.onError(ex, isOnCallback);
                            LoadingDialog.closeLoadDialog();
                        }
                    });
                }
            });

        }

        @Override
        public int getItemCount() {
            return listmap.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            TextView name, phoneNum, address;
            CheckBox setDefaultAddress;
            LinearLayout delete, editor;
            public MyViewHolder(@NonNull View view) {
                super(view);
                name = (TextView) view.findViewById(R.id.name);
                phoneNum = (TextView) view.findViewById(R.id.phoneNum);
                address = (TextView) view.findViewById(R.id.address);

                setDefaultAddress = (CheckBox) view.findViewById(R.id.setDefaultAddress);

                delete = (LinearLayout) view.findViewById(R.id.delete);
                editor = (LinearLayout) view.findViewById(R.id.editor);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }
}
